<?php
class DocumentParser
{
	
	function isLink($str) {
		return (strpos($str, '[link:') !== false);
	}	
	
	
/*
	function &parseLink(&$str, $func, $manager, $article_id = false) {
			
		if(strpos($str, '[link:file') !== false) {
			$search = "/\[link:(file)\|(\d+)\]/e";
			$str = preg_replace($search, "call_user_func_array(\$func, array('file', $article_id, '$2'))", $str);	
		}
		
		if(strpos($str, '[link:article') !== false) {
			
			$ids = $manager->getCategoriesRelated($article_id);
			if(!$ids) { return $str; }
			
			foreach($ids as $entry_id => $category_id) {
				$search[]  = '/\[link:article\|'.$entry_id.'\]/';
				$replace[] = '[link:article|'.$entry_id.'|'.$category_id.']';
			}
			
			$str = preg_replace($search, $replace, $str);
			
			$search = "/\[link:(article)\|(\d+)\|?(\d{0,})\]/e";
			$str = preg_replace($search, "call_user_func_array(\$func, array('entry', '$3', '$2'))", $str);		
		}
		
		return $str;
	}
*/
	
	
	function &parseLink(&$str, $func, $manager, $article_id = false) {
			
		if(strpos($str, '[link:file') !== false) {
			$search = "/\[link:(file)\|(\d+)\]/e";
			$str = preg_replace($search, "call_user_func_array(\$func, array('file', $article_id, '$2'))", $str);	
		}
		
/*
		if(strpos($str, '[link:article') !== false) {
			
			foreach($ids as $entry_id => $category_id) {
				$search[]  = '/\[link:article\|'.$entry_id.'\]/';
				$replace[] = '[link:article|'.$entry_id.'|'.$category_id.']';
			}
			
			$str = preg_replace($search, $replace, $str);
			
			$search = "/\[link:(article)\|(\d+)\|?(\d{0,})\]/e";
			$str = preg_replace($search, "call_user_func_array(\$func, array('entry', '$3', '$2'))", $str);		
		}
*/
		
		
		if(strpos($str, '[link:article') !== false) {
			$search = "/\[link:(article)\|(\d+)\]/e";
			$str = preg_replace($search, "call_user_func_array(\$func, array('entry', 'false', '$2'))", $str);		
		}		
		
		
		return $str;
	}	
	

	function isCode($str) {
		//return (stripos($str, '[code') !== false);
        return (stripos($str, '<pre') !== false);
	}
	
	
	function &parseCode(&$str, $path) {
		//return DocumentParser::_parseCodeGeshi($str);
		return DocumentParser::_parseCodeJs($str, $path);
	}

	
	function _parseCodeGeshi($string) {
		require_once('geshi/geshi.php');
		return preg_replace_callback('#\[code=([^\]]*?)\](.*?)\[/code\]#si', array('DocumentParser', '_parseCodeGeshiCallBack'), $string);	
	}		
	
	
	function _parseCodeGeshiCallBack($matches) { 
	
		$language = strtolower(trim($matches[1]));
		$source = &$matches[2];
		$source = strip_tags($source, '<br>');
		$source = preg_replace('#<br\s*/?>#si', "\n", $source);
		$source = trim($source);
		
		$geshi =& new GeSHi($source, $language);
		$geshi->enable_classes();
		$geshi->set_header_type(GESHI_HEADER_PRE);
		$geshi->set_overall_style('color: #000066; border: 1px solid #d0d0d0; background-color: #f0f0f0;', true);
		$geshi->set_line_style('font: normal normal 95% \'Courier New\', Courier, monospace; color: #003030;', 'font-weight: bold; color: #006060;', true);
		$geshi->set_code_style('color: #000020;', 'color: #000020;');		
	    $style = '<style type="text/css">
	    <!--
	    ' .$geshi->get_stylesheet(). '
	    -->
	    </style>';
	    // Enable line numbers. We want fancy line numbers, and we want every 5th line number to be fancy
		$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 5);
		return $style . $geshi->parse_code();
	}	
	
	
	function &_parseCodeJs(&$str, $home_path) {
		
		$s = '<link type="text/css" rel="stylesheet" href="%sclient/jscript/syntax_highlighter/Styles/shCore.css"></link>' . "\n";
		$s = $s . '<link type="text/css" rel="stylesheet" href="%sclient/jscript/syntax_highlighter/Styles/shThemeDefault.css"></link>' . "\n";
		$css = sprintf($s, $home_path, $home_path);
		
		$js = array();
		$s = '<script language="javascript" src="%sclient/jscript/syntax_highlighter/Scripts/shCore.js"></script>';
		$js[] = sprintf($s, $home_path);
		
        $s ='';
        $code_lang = array('Bash','Cpp','CSharp','Css','Delphi','Diff','Groovy','Java','JScript','Perl','Php','Plain','Python','Ruby','Sql','Ruby','Xml');
		foreach($code_lang AS $v) {
			$s = '<script language="javascript" src="%sclient/jscript/syntax_highlighter/Scripts/shBrush%s.js"></script>';
			$js[] = sprintf($s, $home_path, $v);
		}
		$s = '
		<script language="javascript">
            SyntaxHighlighter.config.clipboardSwf = "%sclient/jscript/syntax_highlighter/scripts/clipboard.swf";
		    SyntaxHighlighter.all();
		</script>';
		$js[] = sprintf($s, $home_path);
		
		$str = "\n" . $css . "\n" . $str . "\n" . implode("\n", $js);
		
		return $str;
	}
	
	
	function &parseGlossaryItems(&$string, $glossary, $manager) {
		
		$i = 0;
		$extensions = array('.php', '.asp', '.html', '.htm', '.xml', '.jsp');
		
		$ids = array();
		foreach($glossary as $id => $item) {
			if(strpos(strtolower($string), strtolower($item)) !== false) {
				$ids[] = $id;
			}
		}
		
		// have items
		if($ids) {
			
			$glossary = &$this->stripVars($manager->getGlossaryDefinitions(implode(',', $ids)));
			
			foreach($glossary as $k => $v) {
				
				$preg = "~\b($k)\b(?![^<]*>)~i";
				preg_match_all($preg, $string, $matches);
				$num = ($v['display_once']) ? 1 : count($matches[0]);
				
				for($j=0;$j<$num;$j++){
										
					$id = 'glossary_' . $i++;
					$str = "<span class=\"glossaryItem\" onClick=\"ShowGlossaryItem('%s', '%s');\" onMouseOut=\"HideGlossaryItem('%s');\">$1</span>\n
					 		<span id=\"%s\" style=\"display: none;\"></span>";
					
					//$str = "<span class=\"glossaryItem\" onmouseover=\"return overlib('%s', CAPTION, 'URL', HAUTO, VAUTO, FGCOLOR, '#FFFF99', BGCOLOR, '#CC9900', WIDTH, 300);\" onmouseout=\"return nd();\">";
					//<a class="glossaryItem" href="/knowledgebase/glossary/#URL">URL</a></span>
					
					$search = "~\b(".$matches[0][$j].")\b(?!<\/span>\s)(?![^<]*>)~i";
					$v['definition'] = addslashes(str_replace(array("\r\n", "\n"), ' ', $v['definition']));			
					$replace = sprintf($str, $id, $v['definition'], $id, $id);					
					$string = preg_replace($search, $replace, $string, 1);
				}
			}
		}
		
		//echo "<pre>"; print_r($string); echo "</pre>";
		return $string;
	}
	
	
	function getSummary($str, $num_signs = '150') {
	
		if($num_signs == 'all') { return $str; }
		if(!$num_signs) { return; }
		
		$values = array('<br>', '<BR>',  '< /br>', '<li>', '<p>', '<P>', '  ');
		$str = preg_replace('#<a[^>]*>.*?<\/a>#i', '', $str);
		$str = strip_tags(str_replace($values, ' ', $str));
		//$str = preg_replace('#(http|https|ftp):\/\/\S+#', '', $str);
		
		if(strlen($str) < $num_signs) { return $str; }
		
		return substr($str, 0, strrpos(substr($str, 0, $num_signs), ' ')) . '...';
	}
}
?>